﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace MooseIRC.Client.Server {

    /// <summary>
    /// Session Server serves up IRC Sessions (Status Clients)
    /// </summary>
    internal sealed class SessionServer : Server
    {

        #region [Local Settings]

        /// <summary>
        /// Collects / Manages Status Clients
        /// </summary>
        private List<StatusClient> m_clients;

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize Session Server
        /// </summary>
        public SessionServer()
            : base()
        {
            m_clients = new List<StatusClient>();
            return;
        }

        #endregion

        #region [Status Client registration]

        /// <summary>
        /// When a status client connects
        /// </summary>
        /// <param name="result">Async result</param>
        protected override void onAcceptComplete(IAsyncResult result) {
            Socket client_socket = null;
            try
            {
                client_socket = m_socket_listener.EndAccept(result);
            }
            catch (Exception)
            {
                //This likely wasnt a problem, it was more the server is being shut down.
                return;
            }

            StatusClient client = new StatusClient(client_socket);
            m_clients.Add(client); //collect this client
            //TODO: register event handlers

            //Set up for another connection
            m_socket_listener.BeginAccept(onAcceptComplete, null);
            return;
        }

        #endregion

        #region [Disposable Override]

        /// <summary>
        /// Must inject specific cleanup logic, for when server is destroyed
        /// </summary>
        public override void Dispose()
        {
            //TODO: disconnect and cleanup all clients/sockets...

            base.Dispose();
        }

        #endregion

        //Clients will manage their own messages..., though they may emit signals, to help us manage them...

        ///// <summary>
        ///// When Status Client sends a message
        ///// </summary>
        ///// <param name="result">Async. Result</param>
        //private void onRecieveComplete(IAsyncResult result) {
        //    return;
        //}

        ///// <summary>
        ///// When transmission to Status client is complete
        ///// </summary>
        ///// <param name="result">Async. Result</param>
        //private void onSendComplete(IAsyncResult result) {
        //    return;
        //}

    }
}
